package vn.gq.cd.model;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import vn.gq.app4vn.doctruyen.BuildConfig;
import vn.gq.app4vn.doctruyen.Configuration;
import vn.gq.app4vn.doctruyen.R;
import vn.gq.app4vn.doctruyen.UngDungVietApp;
import vn.gq.app4vn.doctruyen.Configuration.DataMode;
import vn.gq.app4vn.doctruyen.Configuration.EncryptType;
import vn.gq.cd.model.bean.ChapterItem;
import vn.gq.cd.widget.BaseFragment;
import vn.gq.tym.utils.Client;
import vn.gq.tym.utils.DataType;
import vn.gq.tym.utils.IClientListener;
import vn.gq.tym.utils.OnNetworkListener;
import android.app.Activity;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ChapterAdapter extends BaseAdapter {

	public static final int TYPE_ITEM_NORMAL = 0;
	public static final int TYPE_ITEM_SELECTED = 1;
	
	private LayoutInflater mInfalter;
	private Activity activity;
	private List<ChapterItem> mData = new ArrayList<ChapterItem>();
	private BaseFragment mFragment;
	private Typeface tf;
	
	public ChapterAdapter(Activity activity, BaseFragment fragment) {
		mInfalter = LayoutInflater.from(activity);
		this.activity = activity;
		mFragment = fragment;
		tf = Typeface.createFromAsset(activity.getAssets(), "fonts/Roboto-Condensed.ttf");
	}
	
	public String getURLPath(String fileName) {
		if (fileName.startsWith("asset://") || fileName.startsWith("http://") || fileName.startsWith("https://"))
			return fileName;
		if (Configuration.LOAD_DATA_MODE == DataMode.ONLINE) {
			return String.format("%s/%s/data", Configuration.DATA_URL_BASE, fileName);
		}
		return Configuration.MULTI_FOLDER_DATA ? String.format("asset://gzip:%s/data", fileName) : String.format("asset://gzip:files/%s", fileName);
	}
	
	public void load(final String fileName, final Runnable run) {
		
		Client client = new Client(activity);
		if (Configuration.ENCRYPTED_TYPE == EncryptType.ENCRYPT_CHAPTER_ONLY ||
				Configuration.ENCRYPTED_TYPE == EncryptType.ENCRYPT_BOTH) {
			client.setOutputDataType(DataType.GZIP_CIPHER_JSON);
		} else {
			client.setOutputDataType(DataType.JSON);
		}
		
		Log.e("ChapterAdapter", getURLPath(fileName));
		client.executeAndListener(getURLPath(fileName), new IClientListener() {
			
			@Override
			public void onDone(int code, Object result) {
				if (BuildConfig.DEBUG) {
					Log.e("longhoanggiang", ":0 ChapterAdapter load finished");
				}
				if (code == 1) {
					if (mFragment instanceof OnNetworkListener) {
						((OnNetworkListener) mFragment).onNoNetwork();
					}
				}
				if (result != null && result instanceof JSONArray) {
					JSONArray obj = (JSONArray) result;
					try {
						for (int i=0;i<obj.length();i++) {
							JSONObject jsItem = obj.getJSONObject(i);
							ChapterItem chapterItem = new ChapterItem();
							chapterItem.setPath(jsItem.getString(ChapterItem.ITEM_PATH));
							chapterItem.setName(jsItem.getString(ChapterItem.ITEM_NAME));
							mData.add(chapterItem);
						}
						((UngDungVietApp) activity.getApplication()).chapterList = mData;
					} catch(JSONException e) {
						e.printStackTrace();
					}
				}
				activity.runOnUiThread(run);
			}
		});
	}
	
	@Override
	public int getItemViewType(int position) {
		return (position == selectedItemPos) ? TYPE_ITEM_SELECTED : TYPE_ITEM_NORMAL;
	}

	@Override
	public int getViewTypeCount() {
		return 2;
	}

	@Override
	public int getCount() {
		return mData.size();
	}

	@Override
	public Object getItem(int position) {
		return mData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}
	
	public ArrayList<ChapterItem> getData() {
		return (ArrayList<ChapterItem>) mData;
	}
	
	private int selectedItemPos = -1;
	public void setSelectedPos(int position) {
		selectedItemPos = position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			if (getItemViewType(position) == TYPE_ITEM_SELECTED) {
				convertView = mInfalter.inflate(R.layout.list_item_selected_layout, parent, false);
			} else {
				convertView = mInfalter.inflate(R.layout.list_item_layout, parent, false);
			}
			holder = new ViewHolder();
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.titleTextView = (TextView) convertView.findViewById(R.id.itemlist);
		holder.titleTextView.setTypeface(tf);
		ChapterItem chapterItem = (ChapterItem) getItem(position);
		holder.titleTextView.setText(chapterItem.getName());
		return convertView;
	}
	
	private class ViewHolder {
		public TextView titleTextView;
	}

}
